import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-04-22
 * Time: 11:22
 */
public class Solution {
    public Map<Character, String> map = new HashMap<>();
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if(digits.equals("")){
            return res;
        }
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        StringBuilder stringBuilder = new StringBuilder();
        dfs(digits, 0, res, stringBuilder);
        return res;
    }

    private void dfs(String digits, int index, List<String> res, StringBuilder stringBuilder) {
        if(index == digits.length()){
            res.add(stringBuilder.toString());
            return;
        }
        char c = digits.charAt(index);
        String str = map.get(c);
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            stringBuilder.append(ch);
            dfs(digits, index+1, res, stringBuilder);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
    }
}
